<?php
namespace catchAdmin\propaganda\controller;

use app\common\lib\Redis;
use catchAdmin\propaganda\request\PushRequest;
use catchAdmin\propaganda\service\DataAnalysisService;
use catchAdmin\propaganda\service\EdCenterService;
use catchAdmin\smstopic\model\SmsTopic;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\PropagandaEducationHistory as PropagandaEducationHistoryModel;
use app\common\model\PatientInfo as PatientInfoModel;
use catcher\Code;
use catcher\exceptions\FailedException;
use think\response\Json;
use catchAdmin\propaganda\request\DelRequest;
use catchAdmin\propaganda\request\ReportRequest;


class Educationin extends CatchController
{
    /**
     * 列表
     *
     * @time 2020年01月09日
     * @return \think\response\Json
     */
    public function index(): \think\response\Json
    {
        return CatchResponse::paginate((new PatientInfoModel())->getInEducation());
    }

    /**
     * 发送宣教
     *
     * @time 2020年01月09日
     * @param PushRequest $request
     * @return \think\response\Json
     */
    public function push(PushRequest $request): \think\response\Json
    {
        $req = $request->param();


        //患者收案-群发宣教
        if (isset($req['is_all']) && $req['is_all'] === true) {
            //$data = (new PatientInfoModel())->getAllPatientIds($req['group_id'], request()->user()->id, 'id,name,patientId',2);
            if (!isset($req['key'])) {
                throw new FailedException('key不能为空');
            }

            if (!Redis::getInstance()->redis->exists("collect_pids:" . $req['key'])) {
                throw new FailedException('页面缓存已过期，请重新加载页面');
            }

            $collectData = Redis::getInstance()->redis->get("collect_pids:" . $req['key']);
            $pids = json_decode($collectData, true);

            $patientInfo = (new PatientInfoModel())->whereIn('id', $pids)->field(['id' => 'pid','name','patientId'])->select()->toArray();

            $req['pid'] = array_map(function ($v) {
                    return [
                        'pid'       => $v['id'],
                        'name'      => $v['name'],
                        'patientId' => $v['patientId']
                    ];
                    }, $patientInfo);
            Redis::getInstance()->redis->del("collect_pids:" . $req['key']);
        }
        return CatchResponse::success((new EdCenterService())->pushData($req));
    }

    /**
     * 宣教记录
     *
     * @param CatchRequest $request
     * @time 2020年01月09日
     * @return Json
     */
    public function edhistory(CatchRequest $request): Json
    {
        $ret = (new PropagandaEducationHistoryModel())->getList();
        $result = array_merge($ret, ["code" => Code::SUCCESS, "message" => "success"]);
        return json($result);
    }

    /**
     * 宣教记录详情
     *
     * @param CatchRequest $request
     * @time 2023年04月17日
     * @return Json
     */
    public function edhistory_details(CatchRequest $request): Json
    {
        $req = $request->param();

        if (!isset($req['id']) && empty($req['id']))
        {
            throw new FailedException('缺少id参数');
        }
        $ret = (new PropagandaEducationHistoryModel())->getEdhistoryDetails($req);
        $result = array_merge($ret, ["code" => Code::SUCCESS, "message" => "success"]);
        return json($result);
    }

    /**
     * 患者详情-宣教记录
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function patient_ed_center(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success((new PropagandaEducationHistoryModel())->getPatientEd($request->param('pid')));
    }

    /**
     * 获取宣教短信模板
     *
     * @time 2023年04月14日
     * @return \think\response\Json
     */
    public function get_ed_sms_template(): \think\response\Json
    {
        return CatchResponse::success((new SmsTopic())->getConditionDatas(['model' => 'PropagandaEducation'], "id,title"));
    }

    /**
     * 删除宣教计划
     * @time 2023年04月17日
     * @param DelRequest $request
     * @return \think\response\Json
     */
    public function del_edhistory(DelRequest $request): \think\response\Json
    {
        $req = $request->param();
        return CatchResponse::success((new EdCenterService())->delEdhistory($req['id'], $req['job_id'], $req['pushtime']));
    }

    /**
     * 宣教记录-数据分析-数据概况
     * @time 2023年07月04日
     * @param ReportRequest $request
     * @return \think\response\Json
     */
    public function edDataProfiling(ReportRequest $request): \think\response\Json
    {
        $req = $request->param();
        $ret = (new DataAnalysisService())->getDataProfiling($req['department_id']??0, $req['pushTime']);
        return CatchResponse::success($ret);
    }

    /**
     * 宣教记录-数据分析-数据详情
     * @time 2023年07月04日
     * @param ReportRequest $request
     * @return \think\response\Json
     */
    public function edDataAnalysisDetails(ReportRequest $request): \think\response\Json
    {
        $req = $request->param();
        $ret = (new DataAnalysisService())->getDataDetails($req['type']??1, $req['department_id']??0, $req['pushTime']);
        return CatchResponse::success($ret);
    }

    /**
     * 宣教记录-数据分析-排行榜
     * @time 2023年07月05日
     * @param ReportRequest $request
     * @return \think\response\Json
     */
    public function edDataAnalysisRanking(ReportRequest $request): \think\response\Json
    {
        $req = $request->param();
        $ret = (new DataAnalysisService())->getDataRanking($req['type']??1, $req['department_id']??0, $req['pushTime'], $req['around']??0);
        return CatchResponse::success($ret);
    }

    /**
     * 宣教记录-数据分析-折线图
     * @param ReportRequest $request
     * @return \think\response\Json
     */
    public function edDataAnalysisChart(ReportRequest $request): \think\response\Json
    {
        $req = $request->param();
        $ret = (new DataAnalysisService())->getDataChart($req['department_id']??0, $req['pushTime']);
        return CatchResponse::success($ret);
    }
}